﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace CannedHeater.Model
{
    /// <summary>
    /// A serializable version of the default <see cref="KeyValuePair{TKey,TValue}"/>.
    /// </summary>
    /// <typeparam name="TK">Type of keys</typeparam>
    /// <typeparam name="TV">Type of values</typeparam>

    [DataContract]
    public class SerializableKeyValuePair<TK,TV> : IEquatable<SerializableKeyValuePair<TK,TV>>
    {
        [DataMember]
        public TK Key { get; set; }
        [DataMember]
        public TV Value { get; set; }
        [DataMember]
        public bool CompareKeysOnly { get; set; }

        /// <summary>
        /// Constructs SerializableKeyValuePair
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="val">Value</param>
        /// <param name="compareKeysOnly">Defines whether or not only keys should be compared in the implemented Equals method. (default = true)</param>
        public SerializableKeyValuePair(TK key, TV val, bool compareKeysOnly = true)
        {
            Key = key;
            Value = val;
            CompareKeysOnly = compareKeysOnly;
        }

        public bool Equals(SerializableKeyValuePair<TK, TV> other)
        {
            if (CompareKeysOnly)
                return Key.Equals(other.Key);
            else
                return Key.Equals(other.Key) && Value.Equals(other.Value);
        }
    }
}
